/**
 * 根据当前登录用户分组过滤数据
 * @param {Array} route
 * @param {String} group
 * @returns
 */
export const hasPermission = (route, group) => {
    if (route.meta && route.meta.groups) {
        const permission = Array.isArray(route.meta.groups) ? route.meta.groups : route.meta.groups.split('|');
        if (Array.isArray(permission)) {
            return permission.some((o) => group.indexOf(o) >= 0);
        }

        return false;
    }
    return true;
};

/**
 * 根据当前登录用户分组过滤路由
 * @param {Array} arrRouter
 * @param {String} userRoles
 * @returns
 */
export const filterRoutesByGroup = (arrRouter, userRoles) => {
    if (userRoles) {
        const accessedRouters = arrRouter.filter((route) => {
            if (hasPermission(route, userRoles)) {
                if (route.children && route.children.length > 0) {
                    route.children = filterRoutesByGroup(route.children, userRoles);
                }
                return true;
            }
            return false;
        });

        return accessedRouters;
    }

    return arrRouter;
};
